package com.leoandroid.tool.toolsbox.largeread.adapter

import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.leoandroid.tool.toolsbox.largeread.activity.EatActivity
import com.leoandroid.tool.toolsbox.largeread.bean.XingZuo
import com.leoandroid.tool.toolsbox.largeread.databinding.AdapterXingBinding

class XingAdapter(val context: Context): RecyclerView.Adapter<XingAdapter.XingHolder>() {

    val list:MutableList<XingZuo> = mutableListOf()
    var url:String=""

    class XingHolder(val binding: AdapterXingBinding):RecyclerView.ViewHolder(binding.root)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): XingHolder {
        return XingHolder(AdapterXingBinding.inflate(LayoutInflater.from(context),parent,false))
    }

    override fun onBindViewHolder(holder: XingHolder, position: Int) {
        holder.binding.icon.setImageResource(list[position].icon)
        holder.binding.nameTv.text = list[position].name
        holder.binding.monthTv.text = list[position].time
        holder.binding.root.setOnClickListener {
            when (position){
                0 -> {
                    url = "file:///android_asset/constellation/aries.html"//白羊
                }
                1 ->{
                    url =  "file:///android_asset/constellation/taurus.html"//金牛
                }
                2 ->{
                    url =  "file:///android_asset/constellation/gemini.html"//双子
                }

                3 ->{
                    url =  "file:///android_asset/constellation/cancer.html"//巨蟹
                }
                4 ->{
                    url =  "file:///android_asset/constellation/shizi.html"//狮子无
                }
                5 ->{
                    url =  "file:///android_asset/constellation/virgo.html"//处女
                }
                6 ->{
                    url =  "file:///android_asset/constellation/libra.html"//天秤
                }
                7 ->{
                    url =  "file:///android_asset/constellation/scorpio.html"//天蝎
                }
                8 ->{
                    url =  "file:///android_asset/constellation/sagittarius.html"//射手
                }
                9 ->{
                    url =  "file:///android_asset/constellation/capricornus.html"//摩羯
                }
                10 ->{
                    url =  "file:///android_asset/constellation/aquarius.html"//水瓶
                }
                11 ->{
                    url =  "file:///android_asset/constellation/pisces.html"//双鱼
                }
            }
            val intent = Intent(context, EatActivity::class.java)
            intent.putExtra("TITLE",list[position].name)
            intent.putExtra(EatActivity.key,url)
            context.startActivity(intent)
        }

    }

    override fun getItemCount(): Int {
        return list.size
    }

    fun setData(data:MutableList<XingZuo>){
        if(list != null){
            list.addAll(data)
            notifyDataSetChanged()
        }
    }
}